<template>
	<view class="content">
		<view class="title-block">
			<text class="title">俏红娘</text>
			<text class="title-sm">欢迎使用微信授权登录</text>
		</view>
		<view class="img-box">
			<!-- <image :src="require('static/images/login.jpg')" mode=""></image> -->
			<image src="https://friend.tanshishang.cn/assets/img/login.png" mode=""></image>
		</view>
		<view class="mobile-block">
			<button type="primary" class="btntologin" open-type="getUserInfo" @getuserinfo="loginRegister">微信授权登录</button>
			<button type="default" class="cancel" @click="cancel">取消登录</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		data() {
			return {
				code: '',
			};
		},
		computed: {
			...mapState(['hasLogin', 'token', 'userId', 'sessionKey', 'userInfoFromWx', 'openId'])
		},
		onLoad(data) {
			console.log(data);
			if (data.scene) {
				this.code = data.scene;
			}
		},
		methods: {
			...mapActions(['login', 'setOpenidSessionkey', 'setUserInfoFromWx']),
			loginRegister(info) {
				this.$tips.loading()
				uni.login({
					provider: 'weixin',
					success: (res) => {
						console.log('微信登录:', res);
						//2.请求接口登录
						uni.request({
							url: this.$config.API + '/api/user/getWxCode',
							method: "GET",
							data: {
								code: res.code
							},
							success: (res2) => {
								if (res2.data.code == 200) {

									if (res2.data.data.register == 1) { //已注册，获取用户信息
										this.login({
											token: res2.data.data.token,
											userId: res2.data.data.userId
										});
										uni.setStorageSync('userinfo', JSON.stringify(res2.data.data))
										uni.showToast({
											title: '登录成功',
											duration: 1000,
											mask: true
										});
										setTimeout(() => {
											if (this.code) {
												//扫码进入的
												uni.switchTab({
													url: '/pages/index/index'
												})
											} else {
												uni.navigateBack({
													delta: 1
												})
											}

										}, 1000)
									} else {
										// uni.showToast({
										// 	title: '请先获取手机号注册登录!',
										// 	icon: 'none',
										// 	duration: 1000,
										// 	mask: true
										// });
										this.setOpenidSessionkey({
											openId: res2.data.data.openid,
											sessionKey: res2.data.data.session_key
										});
										this.setUserInfoFromWx(info.detail.userInfo);
										console.log('register?code=', this.code);
										setTimeout(() => {
											uni.navigateTo({
												url: 'register?code=' + this.code
											})
										}, 1000)
									}
								} else {
									let msg = res.data.msg ? res.data.msg : '请求失败';
									this.$tips.fail(msg)
								}
							},
							fail: () => {
								this.$tips.fail()
							}
						});
					},
					fail: (err) => {
						this.$tips.fail()
					},
					complete: () => {
						uni.hideLoading();
					}
				});
			},
			cancel() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	.content {
		width: 100vw;
		height: 100vh;
		background: linear-gradient(#E7EBFF 0%, #FFDBF6 100%);

		.title-block {
			padding: 40% 48rpx 80rpx 48rpx;
			display: flex;
			flex-direction: column;

			.title {
				font-size: 56rpx;
				font-weight: 700;
				color: #212121;
				text-align: center;
			}

			.title-sm {
				margin-top: 12rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #7C8389;
				text-align: center;
			}
		}

		.img-box {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;

			image {
				width: 425rpx;
				height: 447rpx;
			}
		}

		.mobile-block {
			margin-top: -25rpx;
			padding: 0 30rpx;

			.btntologin {
				width: 560rpx;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				background: #FE4392;
				border-radius: 44rpx;
			}
		}

		.cancel {
			margin-top: 48rpx;
			width: 560rpx;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			background: #ffffff;
			border-radius: 44rpx;
		}
	}
</style>
